cmake_minimum_required(VERSION 3.15)
project(mypybind LANGUAGES CXX)
include(cmake/myfuns.cmake)

# xxxxxxxxxxxxxxxxxxxxxxxxxxx path
set(MY_ROOT_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "Project root directory")
set(MY_EXPORTS_DIR "${CMAKE_SOURCE_DIR}/export_headers" CACHE PATH "Export headers dir")
set(MY_PATH_BIN "${MY_ROOT_DIR}/bin" CACHE PATH "Binary output directory")
if(CMAKE_CONFIGURATION_TYPES) # 多配置生成器 (如 Visual Studio)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${MY_PATH_BIN})
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MY_PATH_BIN})
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${MY_PATH_BIN})
else() # 单配置生成器（如 Ninja）
    set(OUTPUT_SUBDIR ${CMAKE_BUILD_TYPE})
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${MY_PATH_BIN}/${OUTPUT_SUBDIR}")
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${MY_PATH_BIN}/${OUTPUT_SUBDIR}")
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${MY_PATH_BIN}/${OUTPUT_SUBDIR}")
endif()

# xxxxxxxxxxxxxxxxxxxxxxxxxxx cxx standard
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard")
set(CMAKE_CXX_STANDARD_REQUIRED ON CACHE BOOL "Require C++ standard")
# 为可执行文件设置 RPATH, 需要位于 add_executable 之前
set(CMAKE_INSTALL_RPATH "$ORIGIN" CACHE PATH "Install RPATH")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH OFF CACHE BOOL "Use link path in RPATH")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Export compile commands")

##xxxxxxxxxxxxxxxxxxxxxxxxxxxxx find package
# set(Python3_ROOT_DIR "C:/Users/qingz/AppData/Local/Programs/Python/Python312")
find_package(Python COMPONENTS Interpreter Development)
if(NOT Python_FOUND)
    message(WARNING "[ymy] Cant't find Python")
endif()
find_package(pybind11 REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(spdlog REQUIRED)

##xxxxxxxxxxxxxxxxxxxxxxxxxxxxx pybind11

# 设置构建系统使用的模块名称变量
set(MY_PY_BIND "mypybind")
set(MY_PY_BIND_NAME ${MY_PY_BIND})
# 使用生成表达式根据构建配置设置模块名称和变量
if(MSVC)
    set(MY_PY_BIND_NAME "$<IF:$<CONFIG:Debug>,${MY_PY_BIND}_d,${MY_PY_BIND}>")
endif()


Python_add_library(${MY_PY_BIND}
    cpp/mypybind.cpp
)
my_config_msvc_relwithdebinfo(${MY_PY_BIND})
# 添加编译宏定义
target_compile_definitions(${MY_PY_BIND}
    PRIVATE
        MY_PY_BIND_NAME=${MY_PY_BIND_NAME}
        MY_PY_BIND=${MY_PY_BIND}
)
##
target_include_directories(${MY_PY_BIND}
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${MY_EXPORTS_DIR}
)
## 链接依赖库; 可能需要添加 $env:PYTHONHOME/libs 目录
target_link_directories(${MY_PY_BIND}
    PUBLIC ${Python_LIBRARY_DIRS}
)
target_link_libraries(${MY_PY_BIND}
    PRIVATE
        fmt::fmt
        spdlog::spdlog
        pybind11_all_do_not_use
    PUBLIC
        pybind11::headers
)

set_target_properties(${MY_PY_BIND} PROPERTIES
    INTERPROCEDURAL_OPTIMIZATION ON
    CXX_VISIBILITY_PRESET hidden
    VISIBILITY_INLINES_HIDDEN ON
)


#===================================== 安装(打包) exe, .so, 发给用户
include(cmake/install.cmake)

message(STATUS "Successfully configured Python binding module: ${MY_PY_BIND}")


## cmake --install build --component conan_runtime --prefix /opt/myapp
##--- cpack 打包命令
## cpack -G ZIP -C Debug --config build/Debug/CPackConfig.cmake
